package action;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;

import service.DepartmentService;
import service.EmployeeService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import entity.Department;
import entity.Employee;
import entity.PageBean;

public class EmployeeAction extends ActionSupport implements
		ModelDriven<Employee> {

	private Employee employee = new Employee();
	private HttpSession session = ServletActionContext.getRequest()
			.getSession();

	// 注入service
	private EmployeeService employeeService;
	private DepartmentService departmentService;

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	private int currpage = 1;

	public void setCurrpage(int currpage) {
		this.currpage = currpage;
	}

	public int getCurrpage() {
		return currpage;
	}

	// 接受来自表单的参数
	public Employee getModel() {
		// TODO Auto-generated method stub
		return employee;
	}

	// 跳转到添加页面
	public String add() {
		List<Department> list = departmentService.findAll();
		ActionContext.getContext().getValueStack().set("list", list);

		return "add_success";
	}

	// 保存
	public String save() {
		employeeService.save(employee);

		return "save_success";
	}

	// 跳转到编辑页面,根据id号查询
	public String edit() {
		employee = employeeService.findById(employee.getEid());
		List<Department> list = departmentService.findAll();
		ActionContext.getContext().getValueStack().set("list", list);
		
		return "edit_success";
	}

	// 保存修改
	public String update() {
		Integer dide = employee.getDepartment().getDid();
		Department department = departmentService.findById(dide);
		employee.setDepartment(department);
		employeeService.update(employee);

		return "modify_success";
	}

	public String delete() {
		employee = employeeService.findById(employee.getEid());
		employeeService.delete(employee);
		
		return "delete_success";
	}

	// 登陆
	public String login() {
		Employee employee = employeeService.login(this.employee);

		if (employee != null) {
			session.setAttribute("employee", employee.getEname());
			return SUCCESS;
		} else {
			this.addActionError("登录名或密码错误");
			return INPUT;
		}
	}

	/*
	 * public String query() { List<Employee> employees =
	 * employeeService.queryAll(); if (employees.size() > 0 && employees !=
	 * null) { session.setAttribute("employees", employees); return
	 * "query_success"; } else { return NONE; }
	 * 
	 * }
	 */
	// 分页查询
	public String queryBP() {
		PageBean<Employee> pageBean = employeeService.findByPage(currpage);
		ActionContext.getContext().getValueStack().push(pageBean);

		return "query_success";
	}

	public EmployeeAction() {
		// TODO Auto-generated constructor stub
	}

}
